package com.renwei.yunlong.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.InputFilter;
import android.text.TextWatcher;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.renwei.yunlong.R;
import com.renwei.yunlong.bean.MuchAssignedBean;
import com.renwei.yunlong.global.AppHelper;
import com.zyf.fwms.commonlibrary.base.baseadapter.BaseRecyclerViewAdapter;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by wangqi on 2018/1/30.
 */

public class WorkAssignedAdapter extends BaseRecyclerViewAdapter<MuchAssignedBean> {

    private int TYPE_NORMAL = 0;
    private int TYPE_LEADER = 1;

    private boolean isLeader = true; // 判断是否是主工单

    public WorkAssignedAdapter(Context context) {
        super(context);
    }

    public void setLeader(boolean leader) {
        isLeader = leader;
    }

    @Override
    public int getItemViewType(int position) {
        if (getItem(position).isState() && position >= 1) {
            return TYPE_LEADER;
        } else {
            return TYPE_NORMAL;
        }
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == TYPE_LEADER) {
            return new LeaderHolder(inflater.inflate(R.layout.item_work_assigned_leader, parent, false));
        } else {
            return new NormalHolder(inflater.inflate(R.layout.item_work_assigned_normal, parent, false));
        }

    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {

        final MuchAssignedBean bean = getItem(position);
        if (getItemViewType(position) == TYPE_NORMAL) { // 普通模式
            final NormalHolder normalHolder = (NormalHolder) holder;

            normalHolder.onRefreshView(bean, position);
        } else if (getItemViewType(position) == TYPE_LEADER) { // 显示模式
            final LeaderHolder leaderHolder = (LeaderHolder) holder;
            leaderHolder.onRefreshView(bean, position);
        }
    }

    class NormalHolder extends RecyclerView.ViewHolder implements TextWatcher {

        @Bind(R.id.tv_title)
        TextView tvTitle;
        @Bind(R.id.iv_delete)
        ImageView ivDelete;
        @Bind(R.id.tv_title_person)
        TextView tvTitlePerson;
        @Bind(R.id.iv_person)
        ImageView ivPerson;
        @Bind(R.id.tv_person)
        TextView tvPerson;
        @Bind(R.id.rl_person)
        RelativeLayout rlPerson;
        @Bind(R.id.rl_title)
        RelativeLayout rlTitle;
        @Bind(R.id.tv_input_title)
        TextView tvInputTitle;
        @Bind(R.id.et_input_content)
        EditText etInputContent;
        @Bind(R.id.tv_input_number)
        TextView tvInputNumber;
        private int mPosition;


        public NormalHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }

        public void onRefreshView(MuchAssignedBean bean, final int position) {
            this.mPosition = position;
            if (position == 0) {
                if (isLeader) {
                    if (getItemCount() > 1) {
                        tvTitlePerson.setText("主处理人(必选)");
                    } else
                        tvTitlePerson.setText("处理人(必选)");
                } else {
                    tvTitlePerson.setText("副处理人(必选)");
                }
                rlTitle.setVisibility(View.GONE);
            } else {
                rlTitle.setVisibility(View.VISIBLE);
                tvTitle.setText("副处理人0" + position);
                tvTitlePerson.setText("副处理人(必选)");
            }

            tvPerson.setText(bean.getResponseUserName());
            tvInputTitle.setText("处理意见");
            etInputContent.setFilters(new InputFilter[]{new InputFilter.LengthFilter(100), AppHelper.emojiFilter});

            if (etInputContent.getTag(R.id.tag_textWatcher_data) instanceof TextWatcher) {
                etInputContent.removeTextChangedListener(this);
            }
            etInputContent.setText(bean.getResponseDes());
            tvInputNumber.setText(bean.getInputCount() + "");

            etInputContent.addTextChangedListener(this);
            etInputContent.setTag(R.id.tag_textWatcher_data, this);
            ivDelete.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    etInputContent.setText("");
                    mOnItemClickLitener.onItemClick(view, position);
                }
            });
            rlPerson.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    mOnItemClickLitener.onItemClick(view, position);
                }
            });
        }

        @Override
        public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

        }

        @Override
        public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
        }

        @Override
        public void afterTextChanged(Editable editable) {
            int length = editable.toString().length();
            tvInputNumber.setText(length + "");

            getItem(mPosition).setResponseDes(editable.toString());
            getItem(mPosition).setInputCount(length);
        }
    }

    class LeaderHolder extends RecyclerView.ViewHolder {

        @Bind(R.id.tv_title)
        TextView tvTitle;
        @Bind(R.id.rl_title)
        RelativeLayout rlTitle;
        @Bind(R.id.tv_person)
        TextView tvPerson;
        @Bind(R.id.tv_work)
        TextView tvWork;
        @Bind(R.id.rl_work)
        RelativeLayout rlWork;

        public LeaderHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }

        public void onRefreshView(MuchAssignedBean bean, final int position) {
            rlTitle.setVisibility(View.VISIBLE);
            tvTitle.setText("副处理人0" + position);

            tvPerson.setText(bean.getResponseUserName());
            tvWork.setText(bean.getWorkOrder());
            tvWork.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    mOnItemClickLitener.onItemClick(view, position);
                }
            });
        }
    }


}
